<!doctype html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport"
        content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
</head>

<body>
    <form action="" class="example-form">
        <div>
            <label for="name">名称</label>
            <input type="text" name="name" id="name" placeholder="please input your name">
        </div>
        <div>
            <label for="res">输入</label>
            <textarea type="multipart" name="res" id="res" placeholder="这里是每一次输入的结果"></textarea>
        </div>
    </form>

    <script>

        const arr = [1, 2, 5, 7, 10, 11, 12, 13, 18, 19, 20, 21, 22, 56, 57, 58];
        let index = 0;
        const result = [];
        for (let i = 0; i < arr.length; i++) {
            if (arr[i] + 1 !== arr[i + 1]) {
                const mini = arr.slice(index, i + 1);
                // console.log(index, i+1,mini)
                if (mini.length > 1) {
                    result.push([`${mini[0]}~${mini[mini.length - 1]}`]);
                } else {
                    result.push(mini)
                }
                index = i + 1;
                console.log(index)
            }
        }
        console.log(result); // [1,2],[10,11,12,13],[18,19,20,21,22],[56,57,58]

        document.onmousemove = throttle(function () {
            console.log("函数节流");
        })

        // 函数节流
        function throttle(cd, time = 300) {
            var t = null;
            return function () {
                if (t) return;
                t = setTimeout(() => {
                    cd.call(this);
                    t = null;
                }, time);
            }
        }
        function debounce(fn, delay = 1000) {
            let timer = null;
            return function (...args) {
                console.log(args);
                if (timer) {
                    clearTimeout(timer);
                    timer = null;
                }

                timer = setTimeout(() => {
                    fn.apply(this, args);
                }, delay);
            }
        }

        // //
        // var result = [[]];
        // arr.reduce(function(previousValue, currentValue, currentIndex, arr) {
        //
        //     console.log(previousValue, currentValue, currentIndex, arr)
        //     var tempArray = result[result.length - 1];
        //     if (previousValue + 1 == currentValue) {
        //         if (tempArray.length == 0) {
        //             tempArray.push(previousValue);
        //         }
        //         tempArray.push(currentValue);
        //     } else {
        //         if (tempArray.length > 0) {
        //             result.push([]);
        //         } else {
        //             result[result.length - 1] = [];
        //         };
        //     }
        //     return currentValue;
        // });
        // console.log(result);

        // const arr1 = [1,2,[3,4,[5,6,8], 9], 10]
        // const res = []
        // function fn(par) {
        //     par.filter((value, index, array) => {
        //         if (Array.isArray(value)) {
        //             fn(value)
        //         } else {
        //             res.push(value)
        //         }
        //     })
        //     return res
        // }
        //
        // console.log(fn(arr1))


        const groupArr = [
            {
                code: 1, name: 'a'
            }, {
                code: 1, name: 'x'
            },
            {
                code: 3, name: 'wdx'
            },
            {
                code: 4, name: 'xqx'
            },
            {
                code: 5, name: 'x3312'
            },
            {
                code: 5, name: '333x113312'
            }
        ]
        function group(params, fn) {
            const arrCode = [...new Set(params.map(v => v.code))]

            const res = arrCode.map((icode) => {
                return params.filter((v) => v.code===icode)
            })
            return res
        }
        console.log(group(groupArr, (i) => i.code));


        // 冒泡
        let maopaoArr = [13,34,55,2,4,1,43,99]
        function maopao(params) {
            params.forEach((v, i) => {
                for (let j = i+1; j < params.length; j++) {
                    if (params[i]>params[j]) {
                        [params[i], params[j]] = [params[j], params[i]]
                    }
                
                }
            });
            return params
        }
        console.log(maopao(maopaoArr));

        // 找最大最小
        let getMaxAndMinArr = [13,34,55,2,4,1,43,99]
        function getMaxAndMin(params) {
            let min = params[0]
            let max = 0
            params.forEach((v) => {
                if (v>max) max = v
                if (v<min) min = v
            })
            return max - min 
        }
        console.log(getMaxAndMin(getMaxAndMinArr));

        //    斐波那契
        function feibonaqie(params) {
            if ([1,2].includes(params)) {
                return params;
            } 
            return feibonaqie(params-1) + feibonaqie(params-2)
        }
        console.log(feibonaqie(30));


        // 实现promise，all
        function fn1() {
            return new Promise((resolve, reject) => {
                setTimeout(() => {
                    resolve(1)
                }, 1000)
            })
        }
        function fn2() {
            return new Promise((resolve, reject) => {
                setTimeout(() => {
                    resolve(2)
                }, 3000)
            })
        }
        function PromiseAll(promises) {
            const res = []
            return new Promise((resolve, reject) => {
                promises.forEach((promiseReq) => {
                    promiseReq().then((v) => {
                        res.push(v)
                    })
                })
                resolve(res)
            })
        }
        PromiseAll(fn1, fn2)



    </script>
</body>

</html>